package com.itboye.mengwu.data.repository;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;

import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.utils.BaseErrorListener;
import com.itboye.core.utils.BaseSuccessReqListener;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.ByJsonRequest.Builder;
import com.itboye.mengwu.data.interfaces.IAddressQuery;
import com.itboye.mengwu.data.model.AddAddressSuccessBean;
import com.itboye.mengwu.data.model.AddressAddModel;
import com.itboye.mengwu.data.model.AddressQueryModel;
import com.itboye.mengwu.data.model.PersonDataBean;

public class AddressResitory extends BaseNetRepository implements
		IAddressQuery<AddressQueryModel> {
	String api = "100";
	public static final String ADDRESSQUERY = "BY_Address_queryNoPaging";// 查询收货地址
	public static final String ADDRESSDELETE = "BY_Address_delete";// 删除收货地址
	public static final String ADDRESSUPDATE = "BY_Address_update";// 修改收货地址
	public static final String GETDEFAULTADDR = "BY_Address_getDefault";// 获取默认地址
	public static final String SETDEFAULTADDR = "BY_Address_setDefault";// 设置默认地址
	public static final String GETSINGLEADDR = "BY_Address_getInfo";// 获取单个地址
	final String ADDRESS_ADD = "BY_Address_add";// 添加地址

	public AddressResitory(ICompleteListener listener) {
		// TODO Auto-generated constructor stub
		super(listener);
	}

	@Override
	public void add(AddressQueryModel model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<AddAddressSuccessBean>() {
		}.getType();
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", model.getUid());
		params.put("country", model.getCountry());
		params.put("provinceid", model.getProvinceid());
		params.put("cityid", model.getCityid());
		params.put("areaid", model.getAreaid());
		params.put("detailinfo", model.getDetailinfo());
		params.put("contactname", model.getContactname());
		params.put("mobile", model.getMobile());
		params.put("postal_code", model.getPostal_code());
		params.put("province", model.getProvince());
		params.put("city", model.getCity());
		params.put("area", model.getArea());
		params.put("default", model.getDefault());

		// 目前最简化调用
		// START
		(new Builder<AddAddressSuccessBean>())
				.setTypeVerParamsAndReturnClass(ADDRESS_ADD, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<AddAddressSuccessBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void delete(AddressQueryModel model) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", model.getId());
		params.put("uid", model.getUid());
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ADDRESSDELETE, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void update(AddressQueryModel model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", model.getId());
		params.put("detailinfo", model.getDetailinfo());
		params.put("contactname", model.getContactname());
		params.put("mobile", model.getMobile());
		params.put("postal_code", model.getPostal_code());
		params.put("default", model.getDefault());
		params.put("uid", model.getUid());
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ADDRESSUPDATE, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getInfo(AddressQueryModel model) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<AddressQueryModel> queryYouHuiQuan(AddressQueryModel model) {

		return null;
	}

	@Override
	public void queryAddress(String uid) {
		// ArrayList<AddressQueryModel> addressQueryModels=new
		// ArrayList<AddressQueryModel>();
		// new TypeAdapter< ArrayList<AddressQueryModel>>();

		Type type = new TypeToken<ArrayList<AddressQueryModel>>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		(new Builder<ArrayList<AddressQueryModel>>())
				.setTypeVerParamsAndReturnClass(ADDRESSQUERY, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<ArrayList<AddressQueryModel>>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getDefaultAddr(String uid) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<PersonDataBean>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		(new Builder<PersonDataBean>())
				.setTypeVerParamsAndReturnClass(GETDEFAULTADDR, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<PersonDataBean>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();

	}

	@Override
	public void setDefaultAddr(String uid, String id) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("id", id);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(SETDEFAULTADDR, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getSingleAddr(String uid, String id) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<ArrayList<AddressQueryModel>>() {
		}.getType();

		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("id", id);
		(new Builder<ArrayList<AddressQueryModel>>())
				.setTypeVerParamsAndReturnClass(GETSINGLEADDR, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<ArrayList<AddressQueryModel>>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

}
